GSL = {
	isLoading:false,
	lastLoadId:-1
};
GSL.init = function(){
	GSL.index.init();
}
GSL.showLoading = function(jq_obj){
	jq_obj.html('<img class="loading" src="'+g_webroot+'/img/loading.gif">');
	jq_obj.find("img.loading").css("margin-left",jq_obj.width()/2-30)
							  .css("margin-top",10);
}
GSL.hideLoading = function(jq_obj){
	jq_obj.empty();
}
GSL.showMsg = function(str){
	var _j = $('#message');
	_j.html(str).css('left',($(window).width()-_j.width())/2+'px').show();
	setTimeout(function(){GSL.hideMsg();},3000);
}
GSL.hideMsg = function(){
	$('#message').fadeOut('slow');
}
GSL.login = function(){
	var submitData = function(){
		var username = $.trim($('#j_login_username').val());
		var password = $.trim($('#j_login_password').val());
		if(username.length<0 || password.length<0){
			return;
		}
		$.post(g_webroot+"/users/login",{username:username,password:password},function(o){
			if(o.ret!=0){
				$("#login_box .error_tips").html(o.msg).css('color','#ff0000');
				return;
			}
			$(this).dialog("close");
			location.reload();
		},"json");
	}
	$('#login_box').dialog(
		{'title':'登陆','minWidth':350,'minHeight':220,
		'buttons':{"注册":function(){$(this).dialog("close");GSL.register();},'登陆':function(){submitData();}
	}});
}
GSL.register = function(){
	var submitData = function(){
		var username = $.trim($('#j_regist_username').val());
		var password = $.trim($('#j_regist_password').val());
		var email =  $.trim($('#j_regist_email').val());
		if(username.length<0 || password.length<0){
			return;
		}
		$.post(g_webroot+"/users/register",{username:username,password:password,email:email},function(o){
			if(o.ret!=0){
				$("#register_box .error_tips").html(o.msg).css('color','#ff0000');
				return;
			}
			$(this).dialog("close");
			location.reload();
		},"json");
	}
	$('#register_box').dialog({'title':'注册','minWidth':350,'minHeight':250,
		'buttons':{"提交":function(){submitData();}}
	});
	$('#register_box button').button();
}
GSL.index = {
	loadView : function(){
		var _last = $('div.entry:last');
		if(GSL.lastLoadId==_last.attr('id')){
			GSL.hideLoading($('#scrolloading'));
			return;
		}
		GSL.isLoading = true;
		$.get(g_webroot+"/blogs/ajaxload/"+encodeURIComponent(g_rss_url)+'?idx='+_last.attr('id')+'&from=view',function(html){
				_last.after(html);
				GSL.isLoading = false;
				GSL.lastLoadId = _last.attr('id');
				GSL.hideLoading($('#scrolloading'));
				$('div.entry:last div.twitter').hover(function(){
					$(this).find('.ticon').show();
				},function(){
					 $(this).find('.ticon').hide();
				})
		})
	},
	loadIndex : function(){
		var _last = $('div.entry:last');
		if(GSL.lastLoadId==_last.attr('id')){
			GSL.hideLoading($('#scrolloading'));
			return;
		}
		GSL.isLoading = true;
		$.get(g_webroot+'/blogs/ajaxload?idx='+_last.attr('id')+'&from=index',function(html){
				_last.after(html);
				GSL.isLoading = false;
				GSL.lastLoadId = _last.attr('id');
				GSL.hideLoading($('#scrolloading'));
		})
	},
	loadNewArticle : function(){
		$.get(g_webroot+'/blogs/newarticle',function(html){
			$('#newarticle').html(html);
		})
	},
	bindSelector : function(){
	},
	bindEntry : function(){
		$("#view div.entry").hover(
			function(){
				$(this).addClass('current_entry');
			},
			function(){
				$(this).removeClass('current_entry');
			}
		)
		this.bindScrollLoading();
	},
	bindToggle : function(){
		$('#border_toggle').toggle(
			function(){
				$('#leftside').width(0).hide();
				$('#main').css('margin-left','0px');
				$(this).find('div.toggle_icon').addClass('toggle_icon_hidden');
			},
			function(){
				$('#leftside').width(280).show();
				$('#main').css('margin-left','280px');
				$(this).find('div.toggle_icon').removeClass('toggle_icon_hidden');
			}
		).
		hover(
			function(){
				var color = $('#leftside').width()==0 ? '#ccc #ccc #ccc #FFFFFF' : '#ccc #FFFFFF #ccc #ccc';
				$(this).find('div.toggle_icon')
				.css('border-color',color);
			},
			function(){
				var color = $('#leftside').width()==0 ? '#F0F0F0 #F0F0F0 #F0F0F0 #ccc ' : '#F0F0F0 #ccc #F0F0F0 #F0F0F0';
				$(this).find('div.toggle_icon')
				.css('border-color',color);
			}
		);
	},
	resizeLayout : function(){
		var height = $(window).height();
		$('#jcontainer').height(height-150);
	},
	bindScrollLoading : function(){
		var _ = this;
		$('div#jcontainer').scroll(function(){
						// $('#login').html(Number(GSL.isLoading));
						if((this.scrollTop > (this.scrollHeight - $(this).height()-30))&&!GSL.isLoading){
							GSL.showLoading($('#scrolloading'));
							if($('#view').length > 0){
	           					_.loadView();
							}else if($('#index').length >0){
								_.loadIndex();
							}
						}
        }); 
	},
	initUIComponent : function(){
		$('#default_list').height($(window).height()-200).get(0).scrollTop = $.cookie('scrollTop');
		var icons = {
			header: "ui-icon-circle-arrow-e",
			headerSelected: "ui-icon-circle-arrow-s"
		};
		$("#accordion" ).accordion({active:g_tab_active,fillSpace:true,collapsible: true,icons: icons});
		$('#default_list').css('overflow-x','hidden');
		$('#j_stow').click(function(){
			if(loginUser==""){
				GSL.showMsg('此功能需要登陆后才能使用');
				GSL.login();
			}
			 $.post(g_webroot+"/blogs/stow",{'blog_id':$(this).attr('_bid')},function(o){
				if(o.ret!=0){
					GSL.showMsg(o.msg);
					return;
				};
				GSL.showMsg('已添加到我的收藏');
				$('#my_list').empty().html(o.html);
			},'json')
		}) 
		$('div.twitter').hover(function(){
			$(this).find('.ticon').show();
		},function(){
			 $(this).find('.ticon').hide();
		})
	},
	init : function(){
		this.initUIComponent();
		this.resizeLayout();
		this.bindEntry();
		this.bindSelector();
		this.bindToggle();
		this.loadNewArticle();
	}
}
$(document).ready(GSL.init);
$(window).unload(function(){
	var _sctop = $('#default_list').get(0).scrollTop;
	$.cookie('scrollTop',_sctop);
});
